package com.demo.modules.job.controller;

import com.demo.common.base.R;
import com.demo.modules.job.entity.CronModel;
import com.demo.modules.job.utils.CronUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * cron 表达式生成工具
 */
@RestController
@RequestMapping("/sys/cron")
public class CronController {

    /**
     * 定时任务列表
     */
    @RequestMapping("/getCronStr")
    public R getCronStr(@RequestBody CronModel cronModel) {

        //表达式
        String cron = CronUtil.createCronExpression(cronModel);
        //表达式描述
        String cronDescription = CronUtil.createDescription(cronModel);
        //预计执行时间
        List<String> timeList = CronUtil.seeExcuteTime(cron);
        return R.ok().put("cron", cron).put("cronDescription", cronDescription).put("timeList", timeList);
    }

    /**
     * 最近10次运行时间
     */
    @RequestMapping("/seeExcuteTime")
    public R seeExcuteTime(String cron) {
        //预计执行时间
        List<String> timeList = CronUtil.seeExcuteTime(cron);
        return R.ok(timeList);
    }

}
